<?php if (empty($this->data)) : // ถ้าไม่มีข้อมูล ?>
<div class="success-flash">No Alert Product.</div>
<? else : // ถ้ามีข้อมูล ?>
<div class="accordion">
        <?php $k = 0;
        foreach($this->data as $cust) : ?>
    <h6><a href="#"><?php echo $cust['Customer']['name']; ?></a></h6>
    <div>
        <table>
            <thead>
                <tr>
                    <th class="no">No</th>
                    <th>Site</th>
                    <th>Part no.</th>
                    <th>Stock Code</th>
                    <th width="40px"><span title="Current Stock">C/S</span></th>
                    <th width="40px">Min</th>
                    <th width="40px">Max</th>
                    <th width="80px"><span title="The day this product begin to lower than min stock.">Min Date</span></th>
                    <th width="80px"><span title="The day this product begin to lower than max stock.">Max Date</span></th>
                    <th width="120px"><span title="The day that vendor could dispatch product to customer.">To Replenish Date</span></th>
                </tr>
            </thead>
            <tbody>
                        <?php foreach($cust['Site'] as $site) : ?>
                            <?php $i = 0;
                            foreach($site['CustProductSite'] as $cps) : ?>
                <tr class="<?php echo $cps['current_stock'] <= $cps['min_stock']? 'min-stock':
                                ($cps['current_stock'] < $cps['max_stock']? 'max-stock':
                                    ($cps['current_stock'] >= $cps['max_stock']? 'green': '')); ?>">
                    <td class="center"><span><?php echo $i + 1; ?></span></td>
                    <td><?php echo $site['name']; ?></td>
                    <td><?php echo $cps['Product']['part_no']; ?></td>
                    <td><?php echo $cps['stock_code']; ?></td>
                    <td class="text-right"><?php echo $cps['current_stock']; ?></td>
                    <td class="text-right"><?php echo $cps['min_stock']; ?></td>
                    <td class="text-right"><?php echo $cps['max_stock']; ?></td>
                                    <?php if ($cps['current_stock'] >= $cps['max_stock']) : ?>
                    <td colspan="3">
                        <div id="divMaxStockNote<?php echo $k; ?>" class="wide"><?php echo empty($cps['max_stock_note'])? '...': $cps['max_stock_note']; ?></div>
                                            <?php echo $ajax->editor("divMaxStockNote{$k}", "/cust_product_sites/inplace_editor/max_stock_note/{$cps['id']}", array('highlightendcolor' => '#7FFF3F', 'formClassName' => 'wide', 'clickToEditText' => 'Click here to edit max stock reason.', 'size' => 30)); ?>
                    </td>
                                    <?php else : ?>
                    <td class="text-right"><?php echo $cps['min_stock_date']; ?></td>
                    <td class="text-right"><?php echo $cps['max_stock_date']; ?></td>
                    <td class="text-right">
                        <span id="spanReplenishDate<?php echo $k; ?>"><?php echo $cps['replenish_date']; ?></span>
                                            <?php
                                            echo $form->hidden('replenish_date', array('id' => 'replenishDate' . $k));
                                            echo $html->link('Replenish date', array('controller' => 'cust_product_sites', 'action' => 'set_replenish_date', $cps['id']), array('id' => 'linkReplenishDate' . $k, 'class' => 'hide'), null, false);
                                            ?>
                    </td>
                                    <?php endif; ?>
                </tr>
                                <?php if ($cps['current_stock'] < $cps['max_stock']) : ?>
            <script type="text/javascript">
                $j(function() {
                    // Event การ set replenish date
                    var element = '#replenishDate<?php echo $k; ?>';
                    var span = '#spanReplenishDate<?php echo $k; ?>';
                    var link = '#linkReplenishDate<?php echo $k; ?>';
                    $j(element).datepicker($j.extend(datepicker_options, {showOn: 'button', buttonImage: base + '/img/icon/edit.png', buttonImageOnly: true,
                        onSelect: function(dateText) {
                            $j(span).update($j(link).attr('href') + '/' + dateText + '');
                        },
                        showButtonPanel: true,
                        closeText: 'Clear'
                    }));
                });
                <?php endif; ?>
            </script>
                            <?php ++$i;
                            ++$k;
                        endforeach; ?>
                    <?php endforeach; ?>
            </tbody>
        </table>
    </div>
        <?php endforeach; ?>
</div>
<br />
<div class="caption">
    <span class="min-stock rect"></span> Current Stock is less than or equal Min Stock.
    <span class="max-stock rect"></span> Current Stock is less than Max Stock.
    <span class="green rect"></span> Current Stock is higher than Max Stock.
</div>

<script type="text/javascript">
    $j(function() {
        $j(".accordion").accordion({
            autoHeight: false,
            collapsible: true,
            active: false
        });

        // เก็บ element ที่จะ clear element replenish date
        var clearRDate = {};
        $j('div[id^=divMaxStockNote]').handover();
        $j('.ui-datepicker-trigger').handover();
        $j('.ui-datepicker-trigger').bind('click', function() {
            clearRDate.link = $j(this).siblings('a').attr('href');
            clearRDate.span = '#' + $j(this).siblings('span').attr('id');
        });

        // ป่มเคลียร์บนปฏิทิน replenish_date clear ค่าวันที่
        $j('.ui-datepicker-close').live('click', function(event) {
            $j(clearRDate.span).update(clearRDate.link);
        });
    });
</script>
<? endif; ?>
<?php //debug($this->data); ?>
